package com.wzx.user.common;

import com.wzx.common.exception.LeadNewsException;
import com.wzx.user.constants.BusinessCon;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author wzx
 * @version 1.0
 * @date 2023/10/15 - 1:46
 * @description 标题
 */
@Slf4j
@Component
public class RedisPwdErrorNumCommon {

    @Autowired
    private RedisTemplate redisTemplate;

    //支付密码
    private static final String REDIS_PAYMENT_PWD_NUM = "REDIS_PAYMENT_PWD_NUM:";
    private static final String REDIS_PAYMENT_PWD_TIME = "REDIS_PAYMENT_PWD_TIME:";

    //登录密码
    private static final String REDIS_LOGIN_PWD_NUM = "REDIS_LOGIN_PWD_NUM:";
    private static final String REDIS_LOGIN_PWD_TIME = "REDIS_LOGIN_PWD_TIME:";

    private static final String REDIS_PAYMENT_YZM_NUM = "REDIS_PAYMENT_YZM_NUM:";

    /**
     * 1，获取，错误次数
     *
     * @param userId
     * @param type
     */
    public void getPwdErrorNum(Integer userId,Integer type) throws LeadNewsException {
        //1，获取前缀
        String redisPrefix = REDIS_PAYMENT_PWD_NUM;
        String redisTimePrefix = REDIS_PAYMENT_PWD_TIME;
        if (type == 2){
            redisPrefix = REDIS_LOGIN_PWD_NUM;
            redisTimePrefix = REDIS_LOGIN_PWD_TIME;
        }

        //2，查询redis中的数据
        String redisKey = redisPrefix + userId;
        Integer paymentPwdNum = (Integer) redisTemplate.opsForValue().get(redisKey);

        //3，判断大小
        if(paymentPwdNum != null && paymentPwdNum >= 3){
            //4，如果错误次数超过三，则查询 重新登录时间，如果没有超过时间，则需要等待
            String redisTimeKey = redisTimePrefix + userId;
            Object redisTime = redisTemplate.opsForValue().get(redisTimeKey);
            if (redisTime != null){
                throw new LeadNewsException("密码错误超过3次，请五分钟后重试！");
            }

        }
    }


    public void setPwdErrorNum(Integer userId,Integer type){
        //1，获取前缀
        String redisPrefix = REDIS_PAYMENT_PWD_NUM;
        String redisTimePrefix = REDIS_PAYMENT_PWD_TIME;
        if (type == 2){
            redisPrefix = REDIS_LOGIN_PWD_NUM;
            redisTimePrefix = REDIS_LOGIN_PWD_TIME;
        }

        //2，将原先的自增
        String redisKey = redisPrefix + userId;
        Long increment = redisTemplate.opsForValue().increment(redisKey, 1);

        //3，如果自增后超过3，则需要等待后重新登录
        if(increment >= 3){
            String redisTimeKey = redisTimePrefix + userId;
            redisTemplate.opsForValue().set(redisTimeKey, increment);
            redisTemplate.expire(redisTimeKey, 5, TimeUnit.MINUTES);
        }

    }

    public void closePwdErrorNum(Integer userId,Integer type){
        //1，获取前缀
        String redisPrefix = REDIS_PAYMENT_PWD_NUM;
        if (type == 2){
            redisPrefix = REDIS_LOGIN_PWD_NUM;
        }

        //2，登录成功，删除
        redisTemplate.delete(redisPrefix + userId);
    }


    public void getYzmErrorNum(String phone) throws LeadNewsException {
        String redisPrefix = REDIS_PAYMENT_YZM_NUM;
        String redisKey = redisPrefix + phone;
        Integer paymentPwdNum = (Integer) redisTemplate.opsForValue().get(redisKey);

        if(paymentPwdNum != null && paymentPwdNum>=3){
            redisTemplate.delete(BusinessCon.User.CODE_HEAD + phone);
            throw new LeadNewsException("验证码错误超过3次，验证码已重置！");
        }
    }

    public void setYzmErrorNum(String phone) throws LeadNewsException {
        String redisPrefix = REDIS_PAYMENT_YZM_NUM;
        String redisKey = redisPrefix + phone;
        redisTemplate.opsForValue().increment(redisKey, 1);
    }

    public void closeYzmErrorNum(String phone){
        //1，获取前缀
        String redisPrefix = REDIS_PAYMENT_YZM_NUM;

        //2，登录成功，删除
        redisTemplate.delete(redisPrefix + phone);
    }

}
